import scrapy
from scrapy_02_dangdang.items import Scrapy02DangdangItem


class DangdangSpider(scrapy.Spider):
    name = "dangdang"
    allowed_domains = ["category.dangdang.com"]
    start_urls = ["https://category.dangdang.com/cp01.01.02.00.00.00.html"]

    # https://category.dangdang.com/pg2-cp01.01.02.00.00.00.html
    # https://category.dangdang.com/pg3-cp01.01.02.00.00.00.html

    base_url = 'https://category.dangdang.com/pg'
    page: int = 1

    def parse(self, response):
        print("===================")
        # src = '//ul[@id="component_59"]/li//img/@src'
        # name = '//ul[@id="component_59"]/li//img/@alt'
        # price = '//ul[@id="component_59"]/li//p[@class="price"]/span[1]/text()'
        li_list = response.xpath('//ul[@id="component_59"]/li')
        for li in li_list:
            # 图片懒加载
            src = li.xpath('.//img/@data-original').extract_first()
            if not src:
                # 第一张图片 没有 data-original
                src = li.xpath('.//img/@src').extract_first()
            name = li.xpath('.//img/@alt').extract_first()
            price = li.xpath('.//p[@class="price"]/span[1]/text()').extract_first()
            # print(src, name, price)

            book = Scrapy02DangdangItem(src=src, name=name, price=price)

            # 获取一个，就保存到管道
            yield book
        if self.page < 10:
            self.page += 1
            url = self.base_url + str(self.page) + '-cp01.01.02.00.00.00.html'

            yield scrapy.Request(url, callback=self.parse)

